package middlewares

import (
	"card/constant"
	"card/pkg/util"
	"context"
	"github.com/gin-gonic/gin"
)

func LogId() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		logId := ctx.GetHeader(constant.HttpId)
		if logId == "" {
			logId = util.GenLogId()
		}
		ctx.Writer.Header().Set(constant.HttpId, logId)
		ctx.Request = ctx.Request.WithContext(
			context.WithValue(ctx.Request.Context(), constant.LogId, logId),
		)
		ctx.Next()
	}
}
